package com.shoppingmall.Adapter;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;
import com.example.shoppingmall.R;
import com.shopping.tools.FileUntils;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 
 * 照片墙详情评论适配器
 */
public class PhotoWallCommentAdapter extends BaseAdapter {
	private Context context;
	private MyHolder myHolder;
	// private ArrayList<PhotoCommentRowModel> PCRList;
	private ArrayList<HashMap<String, String>> commentlist;
	/**
	 * 键：下载链接 值：图片
	 */
	HashMap<String, SoftReference<Drawable>> imageMap = new HashMap<String, SoftReference<Drawable>>();

	/*
	 * downImage 判断hashmap中有无该图片 有：直接用 没有：下载
	 */
	public void downImage(ImageView view, int positon, String url) {
		// 判断有无图片

		Drawable drawable;
		if ((imageMap.containsKey(url))
				&& ((drawable = imageMap.get(url).get()) != null)) {
			// 有
			view.setImageDrawable(drawable);
		} else {
			// 设置默认图片
			view.setImageResource(R.drawable.creatorheadphoto);
			// 开启异步加载
			DownImageTask down = new DownImageTask(view, positon);
			down.execute(url);// 启动

		}

	}

	/**
	 * 
	 * Params 传入的参数 url Progress Result 反回值类型
	 * 
	 */
	class DownImageTask extends AsyncTask<String, Integer, Drawable> {
		ImageView view;
		int positon;// 当前item下标
		String url;

		public DownImageTask(ImageView view, int positon) {
			this.view = view;
			this.positon = positon;
		}

		@Override
		protected Drawable doInBackground(String... params) {
			// 执行耗时操作的--下载图片
			url = params[0];
			return FileUntils.getFileUntils().getImageDownload(url);
			// return FileUntil.getFileUntils(context).downImage(url);
		}

		@Override
		protected void onPostExecute(Drawable result) {
			// 耗时操作结束需要执行--加载图片
			imageMap.put(url, new SoftReference<Drawable>(result));
			view.setImageDrawable(result);

		}

	}

	public PhotoWallCommentAdapter(Context context,
			ArrayList<HashMap<String, String>> commentlist) {
		this.context = context;
		this.commentlist = commentlist;
	}

	public int getCount() {
		return commentlist.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return commentlist.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		myHolder = null;
		if (convertView == null) {
			myHolder = new MyHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.photowall_comment_item, null);
			myHolder.txt_creator = (TextView) convertView
					.findViewById(R.id.txt_creator);
			myHolder.txt_commentcontent = (TextView) convertView
					.findViewById(R.id.txt_commentcontent);
			myHolder.txt_createTime = (TextView) convertView
					.findViewById(R.id.txt_createTime);
			myHolder.img_creatorheadphoto = (ImageView) convertView
					.findViewById(R.id.img_creatorheadphoto);
			convertView.setTag(myHolder);// 把myholder对象放到convertView中
		} else {
			myHolder = (MyHolder) convertView.getTag();
		}
		String headPhotoUrl = commentlist.get(position).get("headPhoto");// 图片的路径
		downImage(myHolder.img_creatorheadphoto, position + 1, headPhotoUrl);
		myHolder.txt_creator.setText(commentlist.get(position).get(
				"creator_name"));
		myHolder.txt_commentcontent.setText(commentlist.get(position).get(
				"comment_content"));
		myHolder.txt_createTime.setText(commentlist.get(position).get(
				"comment_createTime"));
		return convertView;
	}

	final class MyHolder {
		TextView txt_creator, txt_commentcontent, txt_createTime, tv_id;
		ImageView img_creatorheadphoto;

	}

}
